package com.mycat.alipay.server;

import com.alipay.api.internal.util.StringUtils;
import com.mycat.alipay.bizcontent.AppPaymentContent;
import com.mycat.alipay.config.AlipayConfig;
import com.mycat.alipay.util.AlipayAppRequest;
import com.mycat.entity.AlipayParam;
import com.mycat.exception.PaymentException;

import java.util.Map;

/**
 * @desc 支付宝APP支付相关服务
 * @author roger
 * @date 2016年9月26日
 */
public class AliPaymentServer {

	/**
	 * 获取支付宝App支付请求参数
	 * 
	 * @param alipayAppBizContent
	 *            业务参数
	 * @return PaymentParam
	 * @throws PaymentException
	 * @throws Exception
	 */
	public static AlipayParam getAlipayAppRequestParam(AppPaymentContent appPaymentContent) throws PaymentException,
			Exception {
		if (null == appPaymentContent) {
			throw new PaymentException("业务参数为空");
		}
		if (StringUtils.isEmpty(appPaymentContent.getSubject())) {
			throw new PaymentException("【subject】参数为空");
		}
		if (StringUtils.isEmpty(appPaymentContent.getOut_trade_no())) {
			throw new PaymentException("【out_trade_no】参数为空");
		}
		if (StringUtils.isEmpty(appPaymentContent.getTotal_amount())) {
			throw new PaymentException("【total_amount】参数为空");
		}

		Map<String, String> sParaTemp = AlipayAppRequest.buildOrderParamMap(appPaymentContent);
		String orderParam = AlipayAppRequest.buildOrderParam(sParaTemp);
		String sign = AlipayAppRequest.getSign(sParaTemp, AlipayConfig.private_key);
		String param = orderParam + "&" + sign;

		AlipayParam paymentParam = new AlipayParam();

		paymentParam.setAlipayStr(param);
		return paymentParam;
	}

}
